package com.woyou.ui.fragment;

import retrofit.RetrofitError;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.woyou.bean.Result;
import com.woyou.bean.UserInfo;
import com.woyou.bean.rpc.OrderDefaultReq;
import com.woyou.service.NetWorkCenter;
import com.woyou.ui.activity.orderdetail.OrderDetailActivity;
import com.woyou.ui.api.RefreshOrderDetailsListener;
import com.woyou.ui.view.SuperLayout;
import com.woyou.utils.Dialog;
import com.woyou.utils.LogUtil;

/**
 * 是否取消订单
 * 
 * @author shenzhou	2015年1月19日
 */
public class CancelOrderLayout extends SuperLayout {
	
	private static final String TAG = "CancelOrderLayout";
	
	private Dialog dialog = new Dialog();
	private TextView messageText;
	private TextView detailText;
	
	private RefreshOrderDetailsListener mListener;
	
	private String oId;
	
	public void show(String oId){
		this.oId = oId;
		this.setVisibility(View.VISIBLE);
		LogUtil.d(TAG, "oId" + oId);
	}
	
	public void dismiss(){
		this.setVisibility(GONE);
	}
	
	public void setListener(RefreshOrderDetailsListener mListener) {
		this.mListener = mListener;
	}
	
	public CancelOrderLayout(Context context) {
		super(context);
		initView();
	}
	
	public CancelOrderLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}
	
	public CancelOrderLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}
	
	private void initView() {
		View view = View.inflate(mContext, R.layout.layout_cancel_order, this);
		view.findViewById(R.id.cancelorder_cancel).setOnClickListener(this);
		view.findViewById(R.id.cancelorder_confirm).setOnClickListener(this);
		view.findViewById(R.id.cancel_rl).setOnClickListener(this);
		messageText = (TextView) findViewById(R.id.tx1);
		detailText = (TextView) findViewById(R.id.tx2);
	}
	
	public void setMassage(String message, boolean isShow){
		messageText.setText(message);
		if(isShow){
			detailText.setVisibility(View.VISIBLE);
		} else {
			detailText.setVisibility(View.GONE);
		}
	}
	
	@SuppressWarnings("rawtypes")
	private void confirmCancel(){
		if ( !NetWorkCenter.isNetworkConnected(getContext()) ) { 
			showToastOnUI("网络根本没连,赶紧检查");
			return;
		}	
		
		executeTask(new Runnable() {
			
			@Override
			public void run() {
				showProgressDialog();
				try {
					OrderDefaultReq orderDefaultReq = new OrderDefaultReq();
					UserInfo userInfo = mUserModel.getUserInfo();
					if(userInfo!=null){
						orderDefaultReq.setuId(userInfo.getuId());
						orderDefaultReq.setPwd(userInfo.getPwd());
					}
					orderDefaultReq.setoId(oId);
					final Result result = mOrderModel.v2_1cancelOrder(orderDefaultReq);
					
					if ( result!=null && (result.getCode()==1 || result.getCode()==2)){
						showToastOnUI(result.getMsg());
						// 发送事件刷新订单详情,暂时取消订单不需要刷新订单详情
						if ( mListener!=null ) {
							mListener.callBack(oId);
						}
						LogUtil.d(TAG, "cancelOrder");
						
					} else if ( result!=null && result.getCode() == -2 ){
						showToastOnUI("很抱歉,服务器又任性了");
						
					} else if( result!=null && result.getCode() == -3 ){
						runOnUI(new Runnable() {
							
							@Override
							public void run() {
								dialog.LoginToast(getContext(), result.getMsg(), OrderDetailActivity.class);
								LogUtil.d(TAG, "cancelOrder");
							}
						});
						
					} else {
						showToastOnUI(result.getMsg());
					}
				} catch (RetrofitError e) {
					showToastOnUI("很抱歉,服务器又任性了");
					switch (e.getKind()) {
					case HTTP:
						return;
					case CONVERSION:
						return;
					case NETWORK:
						return;
					case UNEXPECTED:
						return;
					}
				} finally {
					dismissProgressDialog();
				}
			}
		});
	}
	
	/**
	 * 防止View的事件穿透
	 */
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancelorder_confirm:
			confirmCancel();
			this.setVisibility(View.GONE);
			break;
			
		case R.id.cancelorder_cancel:
			this.setVisibility(View.GONE);
			break;
			
		case R.id.cancel_rl:
			this.setVisibility(View.GONE);
			break;
		}
	}
	

}
